﻿ 

using System;
using System.Drawing;
using System.Net;
using System.Text.RegularExpressions;
using NTTDATA.BEENChina.ResultCertification.Client.Entry.ServiceEntry;

namespace NTTDATA.BEENChina.ResultCertification.Client.Entry.Logic
{
    public class LoginHelper
    {
        
        private LoginHelper()
        {
           
        }

        private static LoginHelper loginHelper;

        public  static LoginHelper  GetInstance()
        {
            if (loginHelper ==null)
            {
                loginHelper = new LoginHelper();
            }

            return loginHelper;
        }

        public enum LoginType
        {
            /// <summary>
            /// 用户名为空 -1
            /// </summary>
            UserIsEmpty = -1,
            /// <summary>
            /// 用户名输入不合法 -2
            /// </summary>
            UserInputErr = -2,
            /// <summary>
            /// 密码为空 -3
            /// </summary>
            PassIsEmpty = -3,
            /// <summary>
            /// 密码输入不合法 -4
            /// </summary>
            PassInputErr = -4,
            /// <summary>
            /// IP验证失败 -5
            /// </summary>
            IPCheckErr = -5,
            /// <summary>
            /// 口令验证失败 -6
            /// </summary>
            LoginCheckErr = -6,
            /// <summary>
            /// 验证成功 1
            /// </summary>
            LoginSuccess = 1,
            /// <summary>
            /// 是否已登入
            /// </summary>
            IsLogin = 2 //2012-3-20 by lanzhi 增加是否已登入验证
        }


       

        private string getIpAddress()
        {
            string ip = null;
            foreach (IPAddress ipAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (ipAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    ip = ipAddress.ToString();
                    break;
                }
            }
            return ip;
        }

        /// <summary>
        /// 检验用户
        /// </summary>
        /// <returns></returns>
        private int CheckUser( )
        {
            string ip = getIpAddress();
            bool bVerfity = false;
            using (ServiceEntryClient client = new ServiceEntryClient())
            {
                bVerfity = client.VerfityOperator(UserID, userPass, ip);
            }

            return bVerfity?1:(int) LoginType.LoginCheckErr;
           
        }



        private string userPass { get; set; }
    

        /// <summary>
        /// 检验密码
        /// </summary>
        /// <returns></returns>
        private int CheckPassword()
        {
            int Result = 1;

            if (string.IsNullOrEmpty(userPass) == true)
            {
                Result = (int)LoginType.PassIsEmpty;
            }

            return Result;
        }

        private int CheckUserID()
        {
            int Result = 1;

            if (string.IsNullOrEmpty(UserID) == true)
            {
                Result = (int)LoginType.UserIsEmpty;
            }


            return Result;
        }


        #region 公有定义

        /// <summary>
        /// 登录帐号
        /// </summary>
        public string UserID { get; set; }
        //joblist 相关信息
        public string OrderDate { get; set; }
        public string VerfityItemId { get; set; }
        public string CategoryId { get; set; }
        public string VerfityJobId { get; set; }

        // 窗体的屏幕坐标
        public Point FormEntryPoint { get; set; }
        public Point FormEntryListPoint { get; set; }


        public int EntryResultFontSize { get; set; }


        /// <summary>
        /// 身份验证
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="pass">密码</param> 
        /// <returns>1 验证成功 否则失败</returns>
        public int Login(string userId, string pass )
        {
            UserID = userId;
            userPass = pass;

            int result = 1;

            int iTemp = CheckUserID();

            if (iTemp == 1)
            {
                iTemp = CheckPassword( );

                if (iTemp == 1)
                {
                    result = CheckUser( );
                }
                else
                {
                    result = iTemp;
                }
            }
            else
            {
                result = iTemp;
            }
            
            return result;
        }

        public int LogOut()
        {
            int effactRow = 0;

            using (ServiceEntryClient client = new ServiceEntryClient())
            {
                effactRow = client.LogOut(UserID);
            }

            return effactRow;
        }

        public int CancelJob()
        {
            int effactRow = 0;

            RemoveImage();

            using (ServiceEntryClient client = new ServiceEntryClient())
            {
                effactRow = client.CancelJob(UserID, VerfityJobId,CategoryId);
            }

            return effactRow;
        }


        public static void RemoveImage()
        {
            string imageFolder = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data");
            if (System.IO.Directory.Exists(imageFolder))
            {
                System.IO.Directory.Delete(imageFolder, true);
            }
        }

        #endregion
    }
}
